using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_forums_ForumListBox : Controls.DataManagerForumControl
{
    protected override void ItemChanged()
    {
        RefreshSelectedValue();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Refresh();
        }
    }

    public bool ShowRootItem
    {
        get
        {
            if (ViewState["ShowRootItem"] != null)
            {
                return Convert.ToBoolean(ViewState["ShowRootItem"]);
            }
            else
            {
                return true;
            }
        }
        set
        {
            ViewState["ShowRootItem"] = value;
        }
    }

    public string RootItemText
    {
        get
        {
            if (ViewState["RootItemText"] != null)
            {
                return ViewState["RootItemText"].ToString();
            }
            else
            {
                return "Home";
            }
        }
        set
        {
            ViewState["RootItemText"] = value;
        }
    }

    public void Refresh()
    {
        ddlForum.Items.Clear();
        if (ShowRootItem)
        {
            ListItem rootItem = new ListItem(RootItemText, "0");
            ddlForum.Items.Add(rootItem);
        }
        AddChildren(null, 1);

        RefreshSelectedValue();
    }

    private void RefreshSelectedValue()
    {
        if (CurrentForum == null)
        {
            if (ShowRootItem)
            {
                ddlForum.SelectedValue = "0";
            }
            else
            {
                if (ddlForum.Items.Count > 0)
                {
                    ddlForum.SelectedIndex = 0;
                }
            }
        }
        else
        {
            ddlForum.SelectedValue = CurrentForum.ID.ToString();
        }

    }



    private void AddChildren(DAL.Forum parentForum, int depth)
    {
        List<DAL.Forum> childForums = DAL.Forums.GetByParent(parentForum);
        if (childForums.Count > 0)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 1; i <= depth; i++)
            {
                sb.Append("- - ");
            }

            foreach (DAL.Forum currentForum in childForums)
            {

                string listItemText = String.Format("{0}{1}", sb.ToString(), currentForum.Name);
                ListItem rootItem = new ListItem(listItemText, currentForum.ID.ToString());
                ddlForum.Items.Add(rootItem);
                AddChildren(currentForum, depth + 1);
            }
        }
    }
    protected void NewForumSelected(object sender, EventArgs e)
    {
        long forumID = 0;
        if (Int64.TryParse(ddlForum.SelectedValue, out forumID))
        {
            CurrentForum = DAL.Forums.Item(forumID);
        }
        else
        {
            CurrentForum = null;
        }
        RaiseForumSelected();
    }
}
